<?php
	$strSelectPendapatanValue = "";
	$strSelectDiscValue = "";
	
	foreach($arrPendapatanCoa as $coa){
		$strSelectPendapatanValue .= $coa->id . ":" . $coa->name .";"; 
	}
	foreach($arrDiscCoa as $coa){
		$strSelectDiscValue .= $coa->id . ":" . $coa->name .";";
	}
	
	$strSelectPendapatanValue = rtrim($strSelectPendapatanValue,";");
	$strSelectDiscValue = rtrim($strSelectDiscValue,";");
?>
<script>
	var editUrl = "index.php?r=keuangan/master/customerItem/edit";
	
	function reloadPage(){		
		$("#tblData").jqGrid("setGridParam",{postData:{},datatype:"json"}).trigger("reloadGrid",[{current:true}]);
		$("#idSrcTblData").val("");
	}
	
	function refreshGrid(){
		$("#tblData").jqGrid("setGridParam",{
				postData:{},
				datatype:"json"
			}).trigger("reloadGrid",[{current:true}]);
		$("#idSrcTblData").val("");		
	}
	
	
	function editRow(id){
		/*		
		var url="index.php?r=keuangan/tu/monthly_fee/form&op=edit&id=" + id;
		var winName = "Form Process";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=450,height=300;
		var replace = true,onCenter = true;
		var w = openWindow(winName,url,width,height,onCenter,specs,replace);
		w.focus();
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				reloadPage();
			}  
		}, 1000);
		*/
		
		var data = $("#tblData").jqGrid("getRowData",id);

		
		$("#id").val(data.id);
		$("#item_desc").val(data.item_desc);
		$("#item_price").val(data.item_price);
		$("#pendapatan_acc_id").val(data.pendapatan_acc_id);
		$("#disc_acc_id").val(data.disc_acc_id);
		if (data.is_active == "No"){
			$("#is_active").prop( "checked", false );	
		}else{
			$("#is_active").prop( "checked", true);
		}
		
		$("#sys_data").val(data.id);				
		$("#dialog-form").dialog("open");
	}
	
	function deleteRow(id){
		if (confirm("Delete data ?")){			
			window.parent.showLoader();			
			var data = $("#tblData").jqGrid("getRowData",id);
			$.ajax({	
				url : editUrl,
				type:"GET",
				async:true,
				data: {op: "del",id:data.id},
				success: function(data) {
					if(data.actionStatus == 0){
						$("#tblData").jqGrid("delRowData",id);			
					}else if(data.actionStatus == 1){
						alert(data.actionMessage);
						printConsole(data.actionError);
					}						
				},
				complete:function(){
					window.parent.hideLoader();
				}
			});			
			
		}
	}
	function printData(){
		var url="index.php?r=akMaster_Report/department";
		var winName = "Master Department";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=500,height=500;
		var replace = false,onCenter = true;
		var w = openWindow(winName,url,width,height,onCenter,specs,replace);
		w.focus();		
	}

	function addTransactionType(){
		$("#dialog-form").dialog("open");
	}
	
	function prcAddTransactionType(){		
		window.parent.showLoader();			
		//var data = $("#tblData").jqGrid("getRowData",id);
		var data = $("#form").serialize();
		printConsole(data);
		//return;
		
		$.ajax({	
			url : editUrl,
			type:"GET",
			async:true,
			data: data,
			success: function(data) {
				printConsole(data);
				if(data.actionStatus == 0){
					location.reload();			
				}else if(data.actionStatus == 1){
					alert(data.actionMessage);
					printConsole(data.actionError);
				}						
			},
			complete:function(){
				window.parent.hideLoader();
			}
		});			
	}
	
	$(function(){		
		$("#tblData").jqGrid({
			url:'index.php?r=keuangan/master/customerItem/allList',
			postData:{},
			datatype: 'json',
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			colNames: ['id','Name','Price','Pendapatan Account','Disc Account','Active','Lock','Action'],
			colModel: [
				{name: 'id', index: 'id', width: 20,hidden:false,sorttype:'int',formatter:'int'},
				{name: 'item_desc', index: 'item_desc', width: 40,search:true},
				{name: 'item_price', index: 'item_price', width: 40,search:true,template:moneyTemplate},
				{name: 'pendapatan_acc_id', index: 'pendapatan_acc_id', width: 40,search:true,
					formatter:"select",
					editoptions: { value: "<?php echo $strSelectPendapatanValue ?>" },
				},
				{name: 'disc_acc_id', index: 'disc_acc_id', width: 40,search:true,
					formatter:"select",
					editoptions: { value: "<?php echo $strSelectDiscValue ?>" },
				},				
				{name: 'is_active', index: 'is_active', width: 30, align: 'center', formatter: 'checkbox'},
				{name: 'sys_data', index: 'sys_data', width: 30, align: 'center', formatter: 'checkbox'},
				{name: 'act', index: 'act', width: 40, align: 'center'},				
			],
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblData',
			viewrecords: true,
			caption: 'Student Items',
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},
			gridComplete: function(){ 
				var ids = jQuery("#tblData").getDataIDs(); 
				for(var i=0;i<ids.length;i++){ 
					var cl = ids[i]; 
					//btnEdit = "<input style='height:22px;width:20px;' type='button' value='E' onclick=jQuery('#rowed2').editRow("+cl+"); ></ids>"; 
					//btnDelete = "<input style='height:20px;width:20px;' type='button' value='S' onclick=jQuery('#rowed2').saveRow("+cl+"); />"; 
					btnEdit = "<img title='edit' class='imgBtn' style='height:20px;width:20px;' src='images/action/Actions-Edit-icon.png' onclick='editRow(" + cl + ")' />"; 
					btnDelete = "<img title='delete' class='imgBtn' style='margin-left:5px;height:20px;width:20px;' src='images/action/Actions-Delete-icon.png' onclick='deleteRow(" + cl + ")' />"; 
					jQuery("#tblData").setRowData(ids[i],{act:btnEdit+btnDelete}) 
				} 
			}
		});
		$("#tblData").jqGrid('navGrid','#navTblData',{add:false,edit:false,search:false,del:false,refresh:false});
		
		//add search text
		$("#tblData").jqGrid('addSearchText',["nama","description"]);

		//Dialog form
		$("#dialog-form").dialog({
			autoOpen: false,
			height: 250,
			width: 500,
			modal: true,
			buttons: {
				"Simpan": prcAddTransactionType,
				Cancel: function() {
					$("#dialog-form").dialog("close");
				}
			},
			close: function() {
				$("#form")[0].reset();
			}
		});
		
		setColumnSelect("pendapatan_acc_id",":",2);	
		setColumnSelect("disc_acc_id",":",2);
	});
</script>

<div style="">
	
	<div style="float:rigth">	
	<?php 
		$this->widget('application.components.widgets.ActionBar', 
			array("menuAlign" => "right", 
				'items' => array(
					array(
						'id' => "btnRefresh",
						'title' => Yii::t("menu","Segarkan"),
						'icon' => "images/action/Action-Refresh-icon.png",
						'callback'=>"reloadPage"
					),
					array(
							'id' => "btnAdd",
							'title' => Yii::t("menu","Add"),
							'icon' => "images/action/Actions-list-add-icon.png",
							'callback'=>"addTransactionType"
					),						
					array(
						'id' => "btnPrint",
						'title' => Yii::t("menu","Cetak"),
						'icon' => "images/action/Actions-document-print-icon2.png",
						'callback'=>"printData"
					),					
					
				)
			)
		);
	?>
	</div>	
	<div style="clear:both"></div>	
	<table id="tblData"><tr></tr></table>
	<div id="navTblData"></div>
	
	<div id="dialog-form" title="Add Transaction Type">
		<form id="form" name="form" action="">
			<div>
				<input type="hidden" id="id" name="id" style="width:20px" value=""/>				 
			</div>
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Name")?></label>
				<label class="form mandatory" style="width:10px">:</label>
				<input type="text" id="item_desc" name="item_desc" style="width:300px" value=""/>				 
			</div>
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Harga")?></label>
				<label class="form mandatory" style="width:10px">:</label>
				<input type="text" id="item_price" name="item_price" class="currency" style="width:150px" value="0"/>
			</div>
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Pendapatan Acc")?></label>
				<label class="form mandatory" style="width:10px">:</label>
				<select id="pendapatan_acc_id" name="pendapatan_acc_id" style="width:308px">
					<?php
						foreach($arrPendapatanCoa as $coa){
							echo "<option value='$coa->id'>".$coa->acc_code ." : ". $coa->name."</option>";
						} 
					?>
				</select>
			</div>
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Disc Acc")?></label>
				<label class="form mandatory" style="width:10px">:</label>
				<select id="disc_acc_id" name="disc_acc_id" style="width:308px">
					<?php
						foreach($arrDiscCoa as $coa){
							echo "<option value='$coa->id'>".$coa->acc_code ." : ". $coa->name."</option>";
						} 
					?>
				</select>
			</div>
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Active")?></label>
				<label class="form mandatory" style="width:10px">:</label>				
				<input type="checkbox" id="is_active" name="is_active" checked/>				 
			</div>
		</form>	
	</div>
</div>